﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace House_World
{
    public class MergeSort : Sort
    {
        public void Sort(Billboard[] array)
        {
            Mergesort(0, array.Length, array);
        }

        void Mergesort(int p, int r, Billboard[] array)
        {
            if (p < r - 1)
            {
                int q = (p + r) / 2;
                Mergesort(p, q, array);
                Mergesort(q, r, array);
                Merge(p, q, r, array);
            }
        }

        void Merge(int p, int q, int r, Billboard[] array)
        {
            int i = p;
            int j = q;
            int k = 0;

            Billboard[] w = new Billboard[r - p];

            while (i < q && j < r)
            {
                if (array[i].module > array[j].module)
                {
                    w[k++] = array[i++];
                }
                else
                {
                    w[k++] = array[j++];
                }

            }

            while (i < q)
            {
                w[k++] = array[i++];
            }

            while (j < r)
            {
                w[k++] = array[j++];
            }

            for (i = p; i < r; i++)
                array[i] = w[i - p];
        }
    }
}
